/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.sistemas.sse.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.TransactionException;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import pe.edu.sistemas.sse.dao.IJefeOcaaDao;
import pe.edu.sistemas.sse.entity.JefeOcaa;
import pe.edu.sistemas.sse.util.HibernateUtil;

/**
 *
 * @author laptop-w8
 */
@Repository("JefeOcaaDao")
public class JefeOcaaDao implements IJefeOcaaDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean agregarJefeOcaa(JefeOcaa jefeOcaa) {
        return HibernateUtil.agregarEntidad(sessionFactory, jefeOcaa);
    }

    @Override
    public boolean actualizarJefeOcaa(JefeOcaa jefeOcaa) {
        return HibernateUtil.actualizarEntidad(sessionFactory, jefeOcaa);
    }

    @Override
    public JefeOcaa obtenerJefeOcaa(JefeOcaa jefeOcaa) {
        JefeOcaa e = null;
        Session session = sessionFactory.openSession();

        try {
            session.getTransaction().begin();
            e = (JefeOcaa) session.get(JefeOcaa.class, jefeOcaa.getId_jefeOcaaDni());
        } catch (ConstraintViolationException he) {
            System.out.println("excepcion01: " + he);
            session.getTransaction().rollback();
        } catch (TransactionException he) {
            System.out.println("excepcion02: " + he);
            session.getTransaction().rollback();
        } catch (HibernateException he) {
            System.out.println("excepcion03: " + he);
            session.getTransaction().rollback();
        } finally {
            session.getTransaction().commit();
            session.close();
        }

        return e;
    }

    @Override
    public boolean existeJefeOcaa(JefeOcaa jefeOcaa) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
